//
//  SAMemberMessageCell.m
//  saasRetail
//
//  Created by mac on 2023/5/29.
//

#import "SAMemberMessageCell.h"
#import "UILabel+NSMutableAttributedString.h"
#import "SASmsAutoSendListModel.h"

@interface SAMemberMessageCell()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *yellowView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) UIButton *switchBtn;

@property (nonatomic, strong) UILabel *sendTimeLab;
@property (nonatomic, strong) UIButton *setTimeBtn;

@end

@implementation SAMemberMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)clickedSwitchBtn:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    if (sender.selected == YES) {
        self.model.enabled = @"1";
    }else {
        self.model.enabled = @"0";
    }
    !self.selectBlock ?: self.selectBlock();
}

-(void)clickedSetTimeBtn:(UIButton *)sender {
    !self.dateBlock ?: self.dateBlock();
}

-(void)setModel:(SASmsAutoSendListModel *)model {
    _model = model;
    self.titleLab.text = model.name;
    self.contentLab.text = model.content;
    self.switchBtn.selected = [model.enabled isEqualToString:@"1"];
    if ([model.name isEqualToString:@"积分清零"]) {
        self.sendTimeLab.hidden = NO;
        self.setTimeBtn.hidden = NO;
        if (self.date.length > 0) {
            self.sendTimeLab.text = [NSString stringWithFormat:@"发送时间：%@",self.date];
        }else {
            self.sendTimeLab.text = @"清零时间到期前三天";
        }
    }else {
        self.sendTimeLab.hidden = YES;
        self.setTimeBtn.hidden = YES;
    }
}

-(void)initUI {
    
    self.backgroundColor = kBgColor;
    [self.contentView addSubview:self.bgView];
    
    [self.bgView addSubview:self.yellowView];
    [self.bgView addSubview:self.titleLab];
    [self.bgView addSubview:self.sendTimeLab];
    [self.bgView addSubview:self.setTimeBtn];
    [self.bgView addSubview:self.contentLab];
    [self.bgView addSubview:self.switchBtn];
    
    [self.contentLab setLineSpace:5];
    
}

-(UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIImageView alloc] initWithFrame:SARectMake(10, 0, kBaseScreenWidth - 20, 150)];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = kScaleNum(5);
        _bgView.layer.masksToBounds = YES;
        _bgView.userInteractionEnabled = YES;
    }
    return _bgView;
}

-(UIView *)yellowView {
    if (!_yellowView) {
        _yellowView = [[UIView alloc] initWithFrame:SARectMake(20, 15, 18, 18)];
        _yellowView.backgroundColor = kColor(254, 198, 22, 1);
        _yellowView.layer.cornerRadius = kScaleNum(5);
        _yellowView.layer.masksToBounds = YES;
    }
    return _yellowView;
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(47, 15, 200, 18)];
        _titleLab.font = kBFont(18);
        _titleLab.textColor = kBlackColor;
        _titleLab.text = @"会员余额消费通知";
    }
    return _titleLab;
}

-(UILabel *)sendTimeLab {
    if (!_sendTimeLab) {
        _sendTimeLab = [[UILabel alloc] initWithFrame:SARectMake(20, 48, 200, 15)];
        _sendTimeLab.font = kFont(15);
        _sendTimeLab.textColor = kPinkColor;
        _sendTimeLab.text = @"发送时间";
    }
    return _sendTimeLab;
}

-(UIButton *)setTimeBtn {
    if (!_setTimeBtn) {
        _setTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _setTimeBtn.frame = SARectMake(kBaseScreenWidth - 150, 43, 110, 25);
        [_setTimeBtn setTitle:@"设置发送时间" forState:UIControlStateNormal];
        [_setTimeBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        _setTimeBtn.titleLabel.font = kFont(15);
        _setTimeBtn.layer.borderColor = [kBuleColor CGColor];
        _setTimeBtn.layer.borderWidth = 1;
        _setTimeBtn.layer.cornerRadius = 5;
        _setTimeBtn.layer.masksToBounds = YES;
        [_setTimeBtn addTarget:self action:@selector(clickedSetTimeBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _setTimeBtn;
}

-(UILabel *)contentLab {
    if (!_contentLab) {
        _contentLab = [[UILabel alloc] initWithFrame:SARectMake(20, 78, 270, 60)];
        _contentLab.font = kFont(15);
        _contentLab.textColor = kGrayColor;
        _contentLab.text = @"尊敬的会员，感谢光临XXXX店铺，本次消费金额：XXX元，已从余额帐户扣除，现帐户余额为：XXXX元。";
        _contentLab.numberOfLines = 0;
    }
    return _contentLab;
}

-(UIButton *)switchBtn {
    if (!_switchBtn) {
        _switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _switchBtn.frame = SARectMake(kBaseScreenWidth - 90, 0, 50, 53);
        [_switchBtn setImage:[UIImage imageNamed:@"gray_switch"] forState:UIControlStateNormal];
        [_switchBtn setImage:[UIImage imageNamed:@"green_switch"] forState:UIControlStateSelected];
        [_switchBtn addTarget:self action:@selector(clickedSwitchBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBtn;
}

@end
